﻿using System;
using System.Collections.Generic;

namespace Com.OpenCode.Framework.Core
{
    /// <summary>
    ///     插件对象模型。
    /// </summary>
    public class Plugin
    {
        public Plugin(Addin addin)
        {
            Properties = new Properties();
            Children = new List<Plugin>();
            Addin = addin;
        }

        public Addin Addin { get; private set; }

        public List<Plugin> Children { get; private set; }

        public string Name { get; set; }

        public Properties Properties { get; private set; }

        public object Owner { get; set; }

        public Plugin Search(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("查询参数name不能为空。");
            }
            if (Name == name)
            {
                return this;
            }
            Plugin searchPlugin = null;
            foreach (Plugin plugin in Children)
            {
                searchPlugin = plugin.Search(name);
                if (searchPlugin != null)
                {
                    break;
                }
            }
            return searchPlugin;
        }

        public Plugin Search(string name, string key, string value)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("查询参数name不能为空。");
            }
            if (Name == name)
            {
                if (Properties.Exist(key))
                {
                    if (Properties.GetString(key) == value)
                    {
                        return this;
                    }
                }
            }
            Plugin searchPlugin = null;
            foreach (Plugin plugin in Children)
            {
                searchPlugin = plugin.Search(name, key, value);
                if (searchPlugin != null)
                {
                    break;
                }
            }
            return searchPlugin;
        }

        /// <summary>
        ///     按照自定义的规则进行排序。
        /// </summary>
        /// <param name="comparer"></param>
        public void Sort(IComparer<Plugin> comparer)
        {
            Children.Sort(comparer);
            foreach (Plugin _Plugin in Children)
            {
                _Plugin.Sort(comparer);
            }
        }

        /// <summary>
        ///  将当前Plugin转换为泛型数组,包含子节点。
        /// </summary>
        /// <returns></returns>
        public List<Plugin> ToList(Predicate<Plugin> match=null)
        {
            List<Plugin> plugins = new List<Plugin> {this};
            foreach (Plugin _Plugin in Children)
            {
                plugins.AddRange(_Plugin.ToList(match));
            }
            if (match!=null)
            {
                return plugins.FindAll(match);
            }
            return plugins;
        }

        public Plugin Copy()
        {
            Plugin plugin = new Plugin(Addin) {Name = Name};
            foreach (KeyValuePair<object, object> keyValuePair in Properties.Dictionary)
            {
                plugin.Properties.Add(keyValuePair.Key, keyValuePair.Value);
            }
            return plugin;
        }

        public void ClearOwner()
        {
            Owner = null;
            foreach (Plugin child in Children)
            {
                child.ClearOwner();
            }
        }

    }
}